package br.com.tarefas.views;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.com.tarefas.POJO.Funcionario;
import br.com.tarefas.dao.FuncionarioDao;

public class JanelaLogin implements ActionListener {

	JFrame janela;
	JPanel panBotoes, panPrincipal, panCampos;
	JTextField txtMatricula, txtSenha;
	JButton login, sair;

	public JanelaLogin() {

		janela = new JFrame("Login");

		panBotoes = new JPanel();
		panCampos = new JPanel();
		panPrincipal = new JPanel();

		txtMatricula = new JTextField();
		txtSenha = new JTextField();

		panPrincipal.setLayout(new BorderLayout());

		panCampos.setLayout(new GridLayout(2, 2));
		panBotoes.setLayout(new GridLayout(1, 2));

		login = new JButton("Login");
		sair = new JButton("Sair");

		login.addActionListener(this);
		sair.addActionListener(this);

		panCampos.add(new JLabel("Matricula: "));
		panCampos.add(txtMatricula);
		panCampos.add(new JLabel("Senha"));
		panCampos.add(txtSenha);

		panBotoes.add(login);
		panBotoes.add(sair);

		panPrincipal.add(panCampos, BorderLayout.CENTER);
		panPrincipal.add(panBotoes, BorderLayout.SOUTH);

		janela.setContentPane(panPrincipal);

		janela.setLocation(400, 250);

		janela.setSize(200, 150);
		janela.setVisible(true);
		janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	public void actionPerformed(ActionEvent e) {

		String cmd;

		cmd = e.getActionCommand();

		if (cmd.equalsIgnoreCase("login")) {
			
			
			
			
			
			if(txtMatricula.getText().equalsIgnoreCase("") || txtSenha.getText().equalsIgnoreCase("")){
				
				JOptionPane.showMessageDialog(null, "Digite a matricula e a senha");
				
			} else {
			
			
			FuncionarioDao dao = new FuncionarioDao();
			Funcionario func = new Funcionario();
			
			
			
		func =	dao.pesquisarFuncionario(txtMatricula.getText());
		
		
		
		if (func.getMatricula().equalsIgnoreCase(txtMatricula.getText()) && func.getSenha().equalsIgnoreCase(txtSenha.getText())) {
			
			new JanelaPrincipal();
			
		} else {
			
			
		
		}
			}
		}
		 else if (cmd.equalsIgnoreCase("sair")) {

			janela.dispose();

		}

	}
}
